const api = require('../../../utils/api.js')
import { $wuxToast } from '../../../dist/index'
let comId;
let value = true;
var questionId ;
Page({

  /**
   * 页面的初始数据
   */
  data: {   
    isDianzanCom:false,
    adminRes: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var question = JSON.parse(options.question)
    console.log(question)
    this.setData({
      question: question
    })
    if(question.comment!=null){
      this.setData({
        adminRes : true
      })
    }
    this.isDianzan(question.id)
    
    this.getComment(question.id)
    
    // this.isLike(this.data.comment)
    // this.getAdminComment(question)
  },

  
  dianzanComFun(e){
    var that = this  
    comId = e.currentTarget.dataset.id;
    console.log(1)
    wx.getStorage({
      key: 'userId',
      success(res){
        console.log(res.data)
        wx.request({
          url: api.addLike,
          method:"POST",
          data:{
            id: comId,
            userId:res.data
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzanCom:true
              }) 
            }
          }
        }) 
      }
    })
        
  },
  cancelComDianzan(e){
    var that = this
    comId = e.currentTarget.dataset.id;
    console.log(comId)
    wx.request({
      url: api.removeLike,
      method:"POST",
      data:{
        id: comId
      },
      success(res){
        if(res.data.errno == 0){
          that.setData({
            isDianzanCom:false
          })
          
        }
      }
    })
  },
  
  isLike(e){
    var that = this
    console.log(e)
    // wx.getStorage({
    //   key: 'userId',
    //   success(res){
    //     wx.request({
    //       url: api.isLike,
    //       method:"POST",
    //       data:{
    //         userId: res.data,
    //         questionId: id
    //       },
    //       success(res){
    //         if(res.data.errno == 0){
    //           that.setData({
    //             isDianzan:true
    //           })
    //         }
    //       }
    //     })
    //   }
    // })
  },
  isDianzan(id){
    var that = this
    wx.getStorage({
      key: 'userId',
      success(res){
        wx.request({
          url: api.isDianzan,
          method:"POST",
          data:{
            userId: res.data,
            questionId: id
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzan:true
              })
            }
          }
        })
      }
    })
  },
  cancelDianzan(){
    var that = this
    var question = this.data.question
    wx.getStorage({
      key: 'userId',
      success(res){
        wx.request({
          url: api.cancelDianzan,
          method:"POST",
          data:{
            questionId: question.id,
            userId: res.data
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzan: false
              })
              that.showToast("取消点赞")
            }
          }
        })
      }
    })
  },
  dianzanFun(){
    var question = this.data.question
    var that = this
    wx.getStorage({
      key: 'userId',
      success(res){
        wx.request({
          url: api.addDianzan,
          method:"POST",
          data:{
            userId: res.data,
            questionId: question.id
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzan: true
              })
              that.showToast("点赞成功")
            }
          }
        })
      }
    })
  },
  showToast(info) {
    $wuxToast().show({
        type: 'success',
        duration: 1500,
        color: '#fff',
        text: info,
        success: () => {}
    })
  },
  onChange(){
    var questionId = this.data.question.id
    wx.navigateTo({
      url: '../../comment/comment?questionId='+questionId,
    })
  }, 
  // getAdminComment(id){
  //   var that = this

  //   // console.log(id.id)
  //   wx.request({
  //     url: api.adminComment,
  //     data:{
  //       id: id.id
  //     },
  //     success(res){
  //       console.log(res.data.data.comment)
  //       if(res.data.errno== 0){
  //         console.log(22)
  //         that.setData({
  //           comment1: res.data.data.comment,
  //           adminRes: true
  //         })
  //       }

  //     }
  //   })
  // },
  getComment(id){
    var that = this
    questionId = this.data.question
    var i = 0
    wx.request({
      url: api.getQuestionComment,
      data:{
        questionId: id
      },
      success(res){
        if(res.data.data.length > 0){
          console.log(res.data.data)
          that.setData({
            comment: res.data.data,
            show: true
          })
          //处理下数据，每个{}中加个字段，isLike
          for(i=0;i<res.data.data.length;i++){
            let key = "isLike" 
            wx.getStorage({
              key: 'userId',
              success(res){
                wx.request({
                  url: api.isLike,
                  method:"POST",
                  data:{
                    userId: res.data,
                    questionId: questionId.id
                  },
                  success(res){
                    if(res.data.errno == 0){
                      value = true; 
                    }
                  }
                })
              }
            })
            res.data.data[i][key] = value;
          }
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var question = this.data.question
    this.getComment(question.id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})